﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AplicacaoReserva.ReservasForms
{
    public partial class CriarReservaForm : Form
    {

        public int idCliente;
        public int idAgencia;
        public int idAloj;
        private ReservaServiceReference.Reserva reservation = new ReservaServiceReference.Reserva();

        public CriarReservaForm()
        {
            InitializeComponent();
        }

        #region funcionalidades

        private void cb_ViagemSoIda_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_ViagemSoIda.Checked == false)
            {
                dtp_ViagemVolta.Enabled = true;
            }
            else 
            {
                dtp_ViagemVolta.Enabled = false;
            }
        }

        private void btn_ProcAloj_Click(object sender, EventArgs e)
        {
            AlojFinder procAloj = new AlojFinder();
            procAloj.MdiParent = MdiParent;
            procAloj.Show();
            
        }

        private void btn_IrProcurarAlojamento_Click(object sender, EventArgs e)
        {
            tabReserva.SelectedIndex = 1;

            try{
                
                ClienteServiceReference.WCFClienteClient wcfCliente = new ClienteServiceReference.WCFClienteClient();
                LocalServiceReference.WCFLocalClient wcfLocal = new LocalServiceReference.WCFLocalClient();

                LocalServiceReference.Local[] locais = wcfLocal.list();
                ClienteServiceReference.Cliente[] clientes = wcfCliente.list();
            

                reservation.IDAgencia = idAgencia;
                foreach (ClienteServiceReference.Cliente c in clientes) {
                    if (c.nome == cmb_ClientesList.SelectedValue.ToString())
                        reservation.IDCliente = c.IDCliente;
                }
                wcfCliente.Close();

                foreach (LocalServiceReference.Local l in locais)
                {
                    if (l.nome == cmb_ViagemDe.SelectedValue.ToString())
                        reservation.IDPartida = l.IDLocal;
                    if (l.nome == cmb_ViagemPara.SelectedValue.ToString())
                        reservation.IDChegada = l.IDLocal;
                }
                wcfLocal.Close();

            
            }
            catch (Exception ex)
            {
                MessageBox.Show("Occoreu um erro:\n" + ex.ToString(), "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void setReservAloj() {
            btn_ReserAloj.Enabled = true;
        }

        #endregion

        

        private void btn_ReserAloj_Click(object sender, EventArgs e)
        {
            

            ReservaServiceReference.WCFReservaClient wcfReserva = new ReservaServiceReference.WCFReservaClient();
            wcfReserva.insert(reservation);

            MessageBox.Show("Reserva criada com sucesso!", "Criar Reserva", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void chb_IncAlojamento_CheckedChanged(object sender, EventArgs e)
        {
            if (chb_IncAlojamento.Checked == true)
            {
                btn_IrProcurarAlojamento.Enabled = true;
            }
            else
            {
                btn_IrProcurarAlojamento.Enabled = false;
            }
        }

        private void CriarReservaForm_Load(object sender, EventArgs e)
        {
            try
            {
                ClienteServiceReference.WCFClienteClient clientes = new ClienteServiceReference.WCFClienteClient();
                LocalServiceReference.WCFLocalClient locais = new LocalServiceReference.WCFLocalClient();
                TipoTransporteServiceReference.WCFTipoTransporteClient tipoTransporte = new TipoTransporteServiceReference.WCFTipoTransporteClient();
                TipoAlojamentoServiceReference.WCFTipoAlojamentoClient tipoAlojamento = new TipoAlojamentoServiceReference.WCFTipoAlojamentoClient();

                foreach (ClienteServiceReference.Cliente c in clientes.list())
                    cmb_ClientesList.Items.Add(c.nome);


                foreach (LocalServiceReference.Local l in locais.list())
                {
                    cmb_AlojDestino.Items.Add(l.nome);
                    cmb_ViagemDe.Items.Add(l.nome);
                    cmb_ViagemPara.Items.Add(l.nome);
                }

                foreach (TipoTransporteServiceReference.TipoTransporte tp in tipoTransporte.list())
                {
                    cmb_TipoTransporte.Items.Add(tp.descricao);
                }

                foreach (TipoAlojamentoServiceReference.TipoAlojamento ta in tipoAlojamento.list())
                {
                    cmb_TipoAloj.Items.Add(ta.descricao);
                }

                clientes.Close();
                locais.Close();
                tipoTransporte.Close();
                tipoAlojamento.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Occoreu um erro:\n" + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_ReservaViagem_Click(object sender, EventArgs e)
        {
            try{
                ReservaServiceReference.Reserva reservation = new ReservaServiceReference.Reserva();
                ClienteServiceReference.WCFClienteClient wcfCliente = new ClienteServiceReference.WCFClienteClient();
                LocalServiceReference.WCFLocalClient wcfLocal = new LocalServiceReference.WCFLocalClient();

                LocalServiceReference.Local[] locais = wcfLocal.list();
                ClienteServiceReference.Cliente[] clientes = wcfCliente.list();
            

            reservation.IDAgencia = idAgencia;
            foreach (ClienteServiceReference.Cliente c in clientes) {
                if (c.nome == cmb_ClientesList.SelectedValue.ToString())
                    reservation.IDCliente = c.IDCliente;
            }
            wcfCliente.Close();

            foreach (LocalServiceReference.Local l in locais)
            {
                if (l.nome == cmb_ViagemDe.SelectedValue.ToString())
                    reservation.IDPartida = l.IDLocal;
                if (l.nome == cmb_ViagemPara.SelectedValue.ToString())
                    reservation.IDChegada = l.IDLocal;
            }
            wcfLocal.Close();

            ReservaServiceReference.WCFReservaClient wcfReserva = new ReservaServiceReference.WCFReservaClient();
            wcfReserva.insert(reservation);

            MessageBox.Show("Reserva criada com sucesso!", "Criar Reserva", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Occoreu um erro:\n" + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
